package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author likun
 * @version V1.0
 * @date 2020/3/10 10:54
 */
@Service(interfaceClass = LkRoleService.class)
@Transactional
public class LkRoleServiceImpl implements LkRoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAllRole() {
        return roleDao.findAllRole();
    }

    /**
     * 更觉角色id修改对应的权限
     *
     * @param currentId
     * @param rolePermissionIds
     */
    @Override
    public void updatePermission4Role(Integer currentId, Integer[] rolePermissionIds) {
        //1.删除角色id对应的所有权限
        roleDao.deletePermissionByRoleId(currentId);
        //2.遍历权限id集合添加数据到中间表
        if (rolePermissionIds != null && rolePermissionIds.length > 0) {
            for (Integer permissionId : rolePermissionIds) {
                Map<String, Integer> map = new HashMap();
                map.put("roleId", currentId);
                map.put("permissionId", permissionId);
                roleDao.addPermissionIdAndRoleId(map);

            }
        }
    }

    /**
     * 添加角色（添加中间表信息）
     *
     * @param role
     * @param rolePermissionIds
     * @param roleMenuIds
     */
    @Override
    public void addRole(Role role, Integer[] rolePermissionIds, Integer[] roleMenuIds) {

        //添加角色表信息
        roleDao.addRole(role);
        Integer roleId = role.getId();
        //添加角色和权限中间表
        if (rolePermissionIds != null && rolePermissionIds.length > 0) {
            for (Integer rolePermissionId : rolePermissionIds) {
                Map<String, Integer> permissionMap = new HashMap<>();
                permissionMap.put("roleId", roleId);
                permissionMap.put("permissionId", rolePermissionId);
                roleDao.addPermissionIdAndRoleId(permissionMap);
            }
        }
        //添加角色和菜单中间表
        if (roleMenuIds != null && roleMenuIds.length > 0) {
            for (Integer roleMenuId : roleMenuIds) {
                Map<String, Integer> menuMap = new HashMap<>();
                menuMap.put("roleId", roleId);
                menuMap.put("menuId", roleMenuId);
                roleDao.addMenuIdAndRoleId(menuMap);
            }
        }
    }

    /**
     * 分页查新角色信息
     * @param pageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 根据id删除角色，及user，permission，menu中间表
     * @param id
     */
    @Override
    public void deleteRole(Integer id) {
        //1.删除role和user的中间表中对应role_id的数据
        roleDao.deleteRoleAndUser(id);
        //2.删除role和permission的中间表中对应role_id的数据
        roleDao.deletePermissionByRoleId(id);
        //3.删除role和menu的中间表中对应role_id的数据
        roleDao.deleteRoleAndMenu(id);
        //4.删除role表中对应role_id的数据
        roleDao.deleteRoleById(id);
    }
}
